﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using OutdoorLib.BaseEntity.Constraction;


public partial class Ctl_SideEditor : System.Web.UI.UserControl
{
    Func<ListViewDataItem, bool> LvUpdCmd = null;    
    private Func<bool> _OnCancelClick;
    private Func<bool> _OnSaveClick;
    public Func<bool> OnCancelClick { get { return _OnCancelClick; } set { _OnCancelClick = value; } }
    public Func<bool> OnSaveClick { get { return _OnSaveClick; } set { _OnSaveClick = value; } }

    private string savePath = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }

    public  string SideId 
    { 
        get 
        { 
            return hfSideID.Value  ; 
        } 
        set 
        { 
           hfSideID.Value  =value ; 
        } 
    }

    private string CurrentID
    {
        get
        {
            return Request.QueryString[GlobalConstants.qID];
        }

    }

    private string CurrentTagName
    {
        get { return Request.QueryString[GlobalConstants.qTagName]; }
    }

    protected virtual ConstractionEntity Constraction
    {
        get
        {

            if (GlobalConstants.IsNewRecord(CurrentID))
                return (ConstractionEntity)AppEnvironment.ObjectFactory.GetNewObject(CurrentTagName, CurrentID);
            else
                return (ConstractionEntity)AppEnvironment.ObjectFactory.GetObject(CurrentID, CurrentTagName);

        }


    }
  
    protected Side Side { 
        get 
        {   
            return (Side)Constraction.SideList[SideId]; 
        } 
    }


    protected List<Side> SideAsCollection
    {
        get 
        {
            List<Side> res = new List<Side>();
            res.Add(Side);
            return res;
        }
    }
    
    protected string ImageUrl 
    {
        get { return GlobalConstants.SidePhotoFolder + @"\" + SideId.Replace("-", string.Empty) + ".jpg"; }  
    }

    protected string OnFullSizeClick
    {
        get
        {
            return string.Empty;
            //return "window.open('PageProcessor.aspx?Page=iFrameOperation_SideBrowse.aspx?" +
            // iAdmin_Page.qConstractionID + "="
            //+ ConstractionID + GlobalConstants.qParamSplitter
            //+iAdmin_Page.qSideID + "=" + Side.ID + "')";                         
        }
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        MydataBind (); 
        
    }

    private void MydataBind()
    {
        //LvSurfaces.DataSource = Side.SurfaceList;
        //if (Side.PriceList != null) lvPriceList.DataSource = Side.PriceList.GetSortList();        
        lvPhoto.DataSource = SideAsCollection;
        lvPriceList.DataSource = SideAsCollection;
        lvPhoto.DataBind();
        
        lvPriceList.DataBind();
    }

    protected int SurfaceCount = 0;
    protected void LvSurfaces_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        SurfaceCount++;

    }

    protected void LvSurfaces_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        ///Получаем движок пообслуживанию событий данного LV
        ListView lv = (ListView)sender;

        switch (e.CommandName)
        {
            case "Edit":
                {
                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    UpdateSurface(e.Item);
                    lv.EditIndex = -1;
                    break;
                }
            case "Delete":
                {
                    //DeleteCustomer(e.CommandArgument as string);
                    break;
                }
            case "Cancel":
                {
                    _OnCancelClick();
                    break;
                }
                
        }

        //lv.DataSource = Side.SurfaceList;
        //lv.DataBind();
    }

    protected void btnRemovePrice_Click(object sender, ImageClickEventArgs e)
    {
        Side.PriceList.Remove(((ImageButton)sender).CommandArgument); 
        BindPriceList();
    }
  
    protected void btnAddPrice_Click(object sender, ImageClickEventArgs e)
    {
    //    Side.PriceList.Add(new Price() { Value = 0 });
    //    BindPriceList();
    }
    private void BindPriceList()
    { 
        ListView lv_val = (ListView)lvPriceList.EditItem.FindControl("lvPriceListValues");
        lv_val.DataSource = Side.PriceList.GetSortList();
        lv_val.DataBind();            
    }

    private bool UpdatePhoto(ListViewDataItem item)
    {
        
        return true;
    }    

    private bool UpdatePriceItem(ListViewItem itm)
    {

        ListView lv = (ListView)itm.FindControl("lvPriceListValues");
        Side.PriceList.Clear();
        foreach (ListViewItem item in lv.Items)
        {
            //string sDateValue = ((TextBox)item.FindControl("txtDateValue")).Text;
            //string sValue = ((TextBox)item.FindControl("txtPriceValue")).Text;
            //Price pr= new Price (){DateValue =Convert.ToDateTime (sDateValue),Value =Convert.ToInt32(sValue)  };
             
            //Side.PriceList.Add (pr);
        }
        return true;
       
    }

    private bool UpdateSurface(ListViewItem item)
    {
        string sGID = ((TextBox)item.FindControl("TxtGID")).Text;
        string id = ((HiddenField)item.FindControl("hfGID")).Value;
        //((Surface)Side.SurfaceList[id]).Number  = sGID;
        
        return true;
    }

        
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ReadForm();
        OnSaveClick();      
    }

    private void ReadForm()
    {
        Side.Litera = txtLitera.Text;

        if(!string.IsNullOrEmpty (txtGRP.Text))
            Side.GRP = Convert.ToDecimal(txtGRP.Text.Replace(".", ","));
        
        if (!string.IsNullOrEmpty (txtOTS.Text)) 
                Side.OTS = Convert.ToDecimal(txtOTS.Text.Replace(".",","));        
        
        Side.SurfaceCount = Convert.ToInt16(txtSurfCount.Text);
        
        Side.Direction = txtDirection.Text;
        
        Side.LookDistance =Convert.ToInt16 ( txtLookDistance.Text);
    }
    

    

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        _OnCancelClick();
    }    

    protected string ImageLoaderSrc    {
        get
        {
            return "PageProcessor.aspx?Page=frmFileLoader.aspx?";
                  //  iAdmin_Page.qSideID +"=" +SideId + GlobalConstants.qParamSplitter + iAdmin_Page.qConstractionID +"="+ConstractionID;
            
        }
    }


    protected  void ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (string.Empty == e.CommandName) return;
        ListView lv = (ListView)sender;
        switch (lv.ID)
        {
            case "lvPhoto":
                LvUpdCmd = UpdatePhoto;
                break;
            case "lvPriceList":
                LvUpdCmd = UpdatePriceItem;
                break;
            case "LvSurfaces":
                LvUpdCmd = UpdateSurface;
                break;            
        }

        switch (e.CommandName)
        {
            case "Edit":
                {
                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Insert":
                {
                    break;
                }
            case "Update":
                {
                    if (LvUpdCmd((ListViewDataItem)e.Item)) lv.EditIndex = -1;                    
                    break;
                }
            case "Delete":
                {                    
                    break;
                }
        }

        switch (lv.ID)
        {

            case "lvPhoto":
                lv.DataSource = SideAsCollection ;
                break;
            case "lvPriceList":
                if (Side.PriceList != null)lv.DataSource = SideAsCollection;
                break;
            default:
                //lv.DataSource = Side.SurfaceList ;
                break;
        }
        lv.DataBind();        
    }

    protected void ItemEditing(object sender, ListViewEditEventArgs e)
    {
        ListView lv = (ListView)sender;
        if (lv.ID.ToLower() == "lvPriceList".ToLower())
        {
            ListView lv_val = (ListView)lv.EditItem.FindControl("lvPriceListValues");
            lv_val.DataSource = Side.PriceList.GetSortList();
            lv_val.DataBind();            
        }

    }

    protected void ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

    }

    
    
}
